﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MinhTuan;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace News
{
    public class NewsItem
    {
        #region Properties

        private Int32 _Item_Id;
        private String _Lang_id;
        private String _Item_Name;
        private String _Item_Title;
        private String _Item_Summary;
        private String _Item_Details;
        private String _Item_Author;
        private DateTime _Item_PostDate;
        private String _Item_PostString;
        private String _Item_Image;
        private String _Item_ImageDescription;
        private String _Item_Keyword;
        private Int32 _Item_View;
        private Int16 _Category_Id;
        private String _Category_Title;
        private String _Category_Name;
        private Boolean _IsHot;
        private Boolean _IsActive;

        public Int32 Id
        {
            get { return _Item_Id; }
            set { _Item_Id = value; }
        }

        public String LangId
        {
            get { return _Lang_id; }
            set { _Lang_id = value; }
        }

        public String Name
        {
            get { return _Item_Name; }
            set { _Item_Name = value; }
        }

        public String Title
        {
            get { return _Item_Title; }
            set { _Item_Title = value; }
        }

        public String Summary
        {
            get { return _Item_Summary; }
            set { _Item_Summary = value; }
        }

        public String Details
        {
            get { return _Item_Details; }
            set { _Item_Details = value; }
        }

        public String Author
        {
            get { return _Item_Author; }
            set { _Item_Author = value; }
        }

        public DateTime PostDate
        {
            get { return _Item_PostDate; }
            set { _Item_PostDate = value; }
        }

        public String PostString
        {
            get { return _Item_PostString; }
            set { _Item_PostString = value; }
        }

        public String Image
        {
            get { return _Item_Image; }
            set { _Item_Image = value; }
        }

        public String Description
        {
            get { return _Item_ImageDescription; }
            set { _Item_ImageDescription = value; }
        }

        public String Keyword
        {
            get { return _Item_Keyword; }
            set { _Item_Keyword = value; }
        }

        public Int32 View
        {
            get { return _Item_View; }
            set { _Item_View = value; }
        }

        public Int16 CatalogId
        {
            get { return _Category_Id; }
            set { _Category_Id = value; }
        }

        public String CatalogTitle
        {
            get { return _Category_Title; }
            set { _Category_Title = value; }
        }

        public String CatalogName
        {
            get { return _Category_Name; }
            set { _Category_Name = value; }
        }

        public Boolean IsHot
        {
            get { return _IsHot; }
            set { _IsHot = value; }
        }

        public Boolean IsActive
        {
            get { return _IsActive; }
            set { _IsActive = value; }
        }

        #endregion

        #region Constructor

        public NewsItem() { }

        public NewsItem(String Name, String Summary, String Details, String Author, DateTime PostDate,
        String Image)
        {
            _Item_Name = Name;
            _Item_Summary = Summary;
            _Item_Details = Details;
            _Item_Author = Author;
            _Item_PostDate = PostDate;
            _Item_Image = Image;
        }

        public NewsItem(String Name, String Summary, String Details, String Author, DateTime PostDate,
        String Image, Int16 CatalogId, String CatalogName, String CatalogTitle)
        {
            _Item_Name = Name;
            _Item_Summary = Summary;
            _Item_Details = Details;
            _Item_Author = Author;
            _Item_PostDate = PostDate;
            _Item_Image = Image;
            _Category_Id = CatalogId;
            _Category_Title = CatalogTitle;
            _Category_Name = CatalogName;
        }

        public NewsItem(String Name, String Summary, String Details, String Author,
        String ImageURL, String ImageDes, Int16 CatalogId, String CatalogName, String CatalogTitle, Boolean IsHot, Boolean isActive)
        {
            _Item_Name = Name;
            _Item_Summary = Summary;
            _Item_Details = Details;
            _Item_Author = Author;
            _Item_PostDate = PostDate;
            _Item_Image = ImageURL;
            _Item_ImageDescription = ImageDes;
            _Category_Id = CatalogId;
            _Category_Title = CatalogTitle;
            _Category_Name = CatalogName;
            _IsActive = isActive;
            _IsHot = IsHot;
        }

        public NewsItem(String Name, String Title, String Summary, String Details, String Author, DateTime PostDate,
            String Image, Int16 CatalogId, String CatalogName, String CatalogTitle)
        {
            _Item_Name = Name;
            _Item_Title = Title;
            _Item_Summary = Summary;
            _Item_Details = Details;
            _Item_Author = Author;
            _Item_PostDate = PostDate;
            _Item_Image = Image;
            _Category_Id = CatalogId;
            _Category_Title = CatalogTitle;
            _Category_Name = CatalogName;
        }

        public NewsItem(Int32 Id, String Name, String Title, String Summary, String Details, String Author, String PostString, String Image, String Description, String Keyword, Int32 View, Int16 CatalogId, String CatalogName, String CatalogTitle, Boolean IsHot)
        {
            _Item_Id = Id;
            _Item_Name = Name;
            _Item_Title = Title;
            _Item_Summary = Summary;
            _Item_Details = Details;
            _Item_Author = Author;
            _Item_PostString = PostString;
            _Item_Image = Image;
            _Item_ImageDescription = Description;
            _Item_Keyword = Keyword;
            _Item_View = View;
            _Category_Id = CatalogId;
            _Category_Title = CatalogTitle;
            _Category_Name = CatalogName;
            _IsHot = IsHot;
        }

        public NewsItem(Int32 Id, String LangId, String Name, String Title, String Summary, String Details, String Author, DateTime PostDate,
            String PostString, String Image, String Description, String Keyword, Int32 View, Int16 CatalogId, String CatalogName, String CatalogTitle, Boolean IsHot, Boolean IsActive)
        {
            _Item_Id = Id;
            _Lang_id = LangId;
            _Item_Name = Name;
            _Item_Title = Title;
            _Item_Summary = Summary;
            _Item_Details = Details;
            _Item_Author = Author;
            _Item_PostDate = PostDate;
            _Item_PostString = PostString;
            _Item_Image = Image;
            _Item_ImageDescription = Description;
            _Item_Keyword = Keyword;
            _Item_View = View;
            _Category_Id = CatalogId;
            _Category_Title = CatalogTitle;
            _Category_Name = CatalogName;
            _IsHot = IsHot;
            _IsActive = IsActive;
        }

        #endregion

        #region Method

        public  static void GetNewsCatalog(Repeater Catalog, String Lang_Id)
        {
            try
            {
                Access.InitAccess();
                Access.mySqlCommand.CommandText = "Sp_News_GetNewsCatalog";
                Access.mySqlCommand.Parameters.Add("@Lang_Id", SqlDbType.NChar).Value = Lang_Id;
                DataSet myDataset = new DataSet();
                SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(Access.mySqlCommand);
                mySqlDataAdapter.Fill(myDataset, "NewsCatalog");
                Access.ClosedAccess();
                Catalog.DataSource = myDataset.Tables[0];
                Catalog.DataBind();
            }
            catch (System.Exception)
            {
                Access.ClosedAccess();
                Catalog.DataSource = new DataTable("NewsCatalog");
                Catalog.DataBind();
            }
        }

        public static DataTable ShowAll(String Lang_Id)
        {
            try
            {
                Access.InitAccess();
                Access.mySqlCommand.CommandText = "Sp_News_GetNewsForIndex";
                Access.mySqlCommand.Parameters.Add("@Lang_Id", SqlDbType.NChar).Value = Lang_Id;
                DataSet myDataset = new DataSet();
                SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(Access.mySqlCommand);
                mySqlDataAdapter.Fill(myDataset, "GetNewsForIndex");
                Access.ClosedAccess();
                return myDataset.Tables[0];
            }
            catch (System.Exception )
            {
                Access.ClosedAccess();
                return new DataTable("GetNewsForIndex");
            }
        }

        public static DataTable GetNewsByCatalog(String Category_Id, String Lang_Id)
        {
            try
            {
                Access.InitAccess();
                Access.mySqlCommand.CommandText = "Sp_News_GetNewsByCatalog";
                Access.mySqlCommand.Parameters.Add("@Category_Id", SqlDbType.TinyInt).Value = Convert.ToInt16(Category_Id.Split('-')[0]);
                Access.mySqlCommand.Parameters.Add("@Lang_Id", SqlDbType.NChar).Value = Lang_Id;
                DataSet myDataset = new DataSet();
                SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(Access.mySqlCommand);
                mySqlDataAdapter.Fill(myDataset, "NewsByCatalog");
                Access.ClosedAccess();
                return myDataset.Tables[0];
            }
            catch (System.Exception )
            {
                Access.ClosedAccess();
                return new DataTable("NewsByCatalog");
            }
        }

        public static Catalog GetCatalogDetails(String CategoryId, String LangId)
        {
            Catalog myCategory = new Catalog();
            try
            {
                Access.InitAccess();
                Access.mySqlCommand.CommandText = "Sp_News_GetCategoryDetail";
                Access.mySqlCommand.Parameters.Add("@Category_Id", SqlDbType.Int).Value = Convert.ToInt16(CategoryId.Split('-')[0]);
                Access.mySqlCommand.Parameters.Add("@Lang_Id", SqlDbType.NChar).Value = LangId;
                SqlDataReader myReader;
                myReader = Access.mySqlCommand.ExecuteReader();
                if (myReader.Read())
                {
                    Int16 Category_Id = Convert.ToInt16(myReader["Category_Id"]);
                    String Category_Name = myReader["Category_Name"].ToString();
                    String Category_Title = myReader["Category_Title"].ToString();
                    String Category_Keyword = myReader["Category_Keyword"].ToString();
                    myCategory = new Catalog(Category_Id, Category_Name, Category_Title, Category_Keyword);
                }
                myReader.Close();
                Access.ClosedAccess();
                return myCategory;
            }
            catch (System.Exception)
            {
                Access.ClosedAccess();
                return myCategory;
            }
        }

        public static  NewsItem GetItemDetails(String ItemId, String LangId)
       {
           NewsItem myItem = new NewsItem();
               try
               {
                   Access.InitAccess();
                   Access.mySqlCommand.CommandText = "Sp_News_GetItemDetails";
                   Access.mySqlCommand.Parameters.Add("@Item_Id", SqlDbType.Int).Value = ItemId.Split('-')[0];
                   Access.mySqlCommand.Parameters.Add("@Lang_Id", SqlDbType.NChar).Value = LangId;
                   SqlDataReader myReader;
                   myReader = Access.mySqlCommand.ExecuteReader();
                   if (myReader.Read())
                   {
                       Int32 Item_Id = (Int32)myReader["Item_Id"];
                       String Lang_Id = myReader["Lang_Id"].ToString();
                       String Item_Name = myReader["Item_Name"].ToString();
                       String Item_Title = myReader["Item_Title"].ToString();
                       String Item_Image = myReader["Item_Image"].ToString();
                       String Item_ImageDescription = myReader["Item_ImageDescription"].ToString();
                       String Item_Summary = myReader["Item_Summary"].ToString();
                       String Item_Details = myReader["Item_Details"].ToString();
                       String Item_Author = myReader["Item_Author"].ToString();
                       String Item_Keyword = myReader["Item_Keyword"].ToString();
                       String Item_PostString = myReader["Item_PostString"].ToString();
                       Int32 Item_View= (Int32)myReader["Item_View"];
                       Int16 Category_Id =  Convert.ToInt16(myReader["Category_Id"]);
                       String Category_Name = myReader["Category_Name"].ToString();
                       String Category_Title = myReader["Category_Title"].ToString();
                       Boolean IsHot = (Boolean)myReader["IsHot"];
                       myItem = new NewsItem(Item_Id, Item_Name, Item_Title, Item_Summary, Item_Details, Item_Author,
                                         Item_PostString, Item_Image, Item_ImageDescription, Item_Keyword, Item_View, Category_Id, Category_Name, Category_Title, IsHot);
                   }
                   myReader.Close();
                   Access.ClosedAccess();
                   return myItem;
               }
               catch (System.Exception )
               {
                   Access.ClosedAccess();
                   return myItem;
               }
               }

        public static void  GetRelativeItems(ListView Relative, Int16 CatalogId, Int32 ItemId, String Lang_Id)
        {
            try
            {
                Access.InitAccess();
                Access.mySqlCommand.CommandText = "Sp_News_GetRelativeItems";
                Access.mySqlCommand.Parameters.Add("@Category_Id", SqlDbType.TinyInt).Value = CatalogId;
                Access.mySqlCommand.Parameters.Add("@Item_Id", SqlDbType.Int).Value = ItemId;
                Access.mySqlCommand.Parameters.Add("@Lang_Id", SqlDbType.NChar).Value = Lang_Id;
                DataSet myDataset = new DataSet();
                SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(Access.mySqlCommand);
                mySqlDataAdapter.Fill(myDataset, "RelativeItems");
                Access.ClosedAccess();
                Relative.DataSource = myDataset.Tables[0];
                Relative.DataBind();
            }
            catch (System.Exception)
            {
                Access.ClosedAccess();
                Relative.DataSource = new DataTable("RelativeItems");
                Relative.DataBind();
            }
        }

        public static void GetHotItems(Repeater HotItems, String Lang_Id)
        {
            try
            {
                Access.InitAccess();
                Access.mySqlCommand.CommandText = "Sp_News_GetHotItem";
                Access.mySqlCommand.Parameters.Add("@Lang_Id", SqlDbType.NChar).Value = Lang_Id;
                DataSet myDataset = new DataSet();
                SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(Access.mySqlCommand);
                mySqlDataAdapter.Fill(myDataset, "HotItems");
                Access.ClosedAccess();
                HotItems.DataSource = myDataset.Tables[0];
                HotItems.DataBind();
            }
            catch (System.Exception)
            {
                Access.ClosedAccess();
                HotItems.DataSource = new DataTable("HotItems");
                HotItems.DataBind();
            }
        }

        public static void GetMostViewItems(Repeater MostViewItems, String Lang_Id)
        {
            try
            {
                Access.InitAccess();
                Access.mySqlCommand.CommandText = "Sp_News_GetMostViewItem";
                Access.mySqlCommand.Parameters.Add("@Lang_Id", SqlDbType.NChar).Value = Lang_Id;
                DataSet myDataset = new DataSet();
                SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(Access.mySqlCommand);
                mySqlDataAdapter.Fill(myDataset, "MostViewItems");
                Access.ClosedAccess();
                MostViewItems.DataSource = myDataset.Tables[0];
                MostViewItems.DataBind();
            }
            catch (System.Exception)
            {
                Access.ClosedAccess();
                MostViewItems.DataSource = new DataTable("MostViewItems");
                MostViewItems.DataBind();
            }
        }
       
        public static void BuildFocusNews( Literal Newest, Repeater relativenews, String Lang_Id)
        {
            try
            {
                Access.InitAccess();
                Access.mySqlCommand.CommandText = "Sp_News_GetFocusItem";
                Access.mySqlCommand.Parameters.Add("@Lang_Id", SqlDbType.NChar).Value = Lang_Id;
                DataSet myDataset = new DataSet();
                SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(Access.mySqlCommand);
                mySqlDataAdapter.Fill(myDataset, "HotItems");
                Access.ClosedAccess();
                DataTable DataSource = myDataset.Tables[0];
                DataRow myRow =DataSource.Rows[0];
                StringBuilder myNewest = new StringBuilder();
                myNewest.Append("<a href = \"http://localhost:13101/Vienthongsaigon/newsdetails/" + myRow["Item_Title"].ToString() + "\" >" + myRow["Item_Name"].ToString() + " </a> \n ");
                myNewest.Append("<span class = \"ppost\" >" + myRow["Item_PostString"].ToString() + "</span><br /> \n ");
                myNewest.Append("<a href = \"http://localhost:13101/Vienthongsaigon/newsdetails/" + myRow["Item_Title"].ToString() + "\" title = \"" + myRow["Item_Name"].ToString().Replace("\"", "") + "\" > \n ");
                myNewest.Append("<img class = \"ifocusimage\" src = \"" + myRow["Item_Image"].ToString() + "\" alt = \"" + myRow["Item_Name"].ToString().Replace("\"", "") + "\" style = \"border: none ;\" /> </a> \n ");
                myNewest.Append("<p style = \"line-height: 180%; text-align: justify;\">" + myRow["Item_Summary"].ToString() + "</p>");
                Newest.Text = myNewest.ToString();
                DataSource.Rows.RemoveAt(0);
                relativenews.DataSource = DataSource;
                relativenews.DataBind();
            }
            catch (System.Exception)
            {
                Access.ClosedAccess();
            }
        }
       
        #endregion

        #region Admin

        public static void GetNewsCatalogForAdmin(DropDownList Catalog, String Lang_Id)
       {
           try
           {
               Access.InitAccess();
               Access.mySqlCommand.CommandText = "Sp_News_GetNewsCatalogForAdmin";
               Access.mySqlCommand.Parameters.Add("@Lang_Id", SqlDbType.NChar).Value = Lang_Id;
               SqlDataReader myReader;
               myReader = Access.mySqlCommand.ExecuteReader();
               Catalog.Items.Clear();
               ListItem myItem;
               while (myReader.Read())
               {
                   myItem = new ListItem(myReader["Category_Name"].ToString(), myReader["Category_Title"].ToString());
                   Catalog.Items.Add(myItem);
               }
               myReader.Close();
               Access.ClosedAccess();
             }
           catch (System.Exception)
           {
               ListItem myItem;
               if (String.Compare(Lang_Id, "vn", true) == 0)
               {
                   myItem = new ListItem("- Chọn danh mục", "-1");
               }
               else
               {
                   myItem = new ListItem("- Select", "-1");
               }
               Catalog.Items.Insert(0, myItem);
           }
       }

        public static string SaveItem(NewsItem myItem,string Lang)
        {
            Int16 Result = 0; 
            try
            {
                String Title = Access.RemoveVietnameseSigns(myItem.Name);
               DateTime myDate = DateTime.Now;
               String PostString = Access.GetDateString(myDate, Lang);
                Access.InitAccess();
                Access.mySqlCommand.Parameters.Clear();
                Access.mySqlCommand.CommandText = "Sp_AddNewsItem";
                Access.mySqlCommand.Parameters.Add("@Lang_id", SqlDbType.NChar).Value = Lang;
                Access.mySqlCommand.Parameters.Add("@Item_Name", SqlDbType.NVarChar).Value = myItem.Name;
                Access.mySqlCommand.Parameters.Add("@Item_Title", SqlDbType.NVarChar).Value = Title;
                Access.mySqlCommand.Parameters.Add("@Item_Summary", SqlDbType.NVarChar).Value = myItem.Summary;
                Access.mySqlCommand.Parameters.Add("@Item_Details", SqlDbType.NText).Value = myItem.Details;
                Access.mySqlCommand.Parameters.Add("@Item_Author", SqlDbType.NVarChar).Value = myItem.Author;
                Access.mySqlCommand.Parameters.Add("@Item_PostDate", SqlDbType.SmallDateTime).Value = myDate;
                Access.mySqlCommand.Parameters.Add("@Item_PostString", SqlDbType.NVarChar).Value = PostString;
                Access.mySqlCommand.Parameters.Add("@Item_Image", SqlDbType.NVarChar).Value = myItem.Image;
                Access.mySqlCommand.Parameters.Add("@Item_ImageDescription", SqlDbType.NVarChar).Value = myItem.Name + ", " + myItem.Description;
                Access.mySqlCommand.Parameters.Add("@Item_Keyword", SqlDbType.NVarChar).Value = myItem.Name + ", " + myItem.CatalogName + ", " + myItem.Summary;
                Access.mySqlCommand.Parameters.Add("@Category_Id", SqlDbType.TinyInt).Value = myItem.CatalogId;
                Access.mySqlCommand.Parameters.Add("@Category_Title", SqlDbType.NVarChar).Value = myItem.CatalogTitle;
                Access.mySqlCommand.Parameters.Add("@Category_Name", SqlDbType.NVarChar).Value = myItem.CatalogName;
                Access.mySqlCommand.Parameters.Add("@IsHot", SqlDbType.NVarChar).Value = myItem.IsHot;
                Access.mySqlCommand.Parameters.Add("@IsActive", SqlDbType.NVarChar).Value = myItem.IsActive;
                Result = Convert.ToInt16(Access.mySqlCommand.ExecuteNonQuery());
                Access.ClosedAccess();
                if (Result == 1)
                {
                   return "Thêm mới bản tin " + myItem.Name + " thành công.";
                }
                return "Thêm mới bản tin " + myItem.Name + " không thành công.";
            }
            catch (Exception)
            {
                Access.ClosedAccess();
                return "Đã xảy ra lỗi trong quá trình thêm mới bản tin " + myItem.Name + ". <br />Vui lòng kiểm tra lại dữ liệu (trùng tên...)";
            }


        }
        
        #endregion


    }
}
